package wei.xinya;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Date;
import java.util.Properties;

public class Work_Order_Producer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "192.168.77.50:9092");
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        try (KafkaProducer<Object, Object> producer = new KafkaProducer<>(props)) {
            String topic = "RTW_DWFN_HZ_WORKER_ORDER_SHZD";
            String key = "key";

            JSONObject mes = new JSONObject();
            mes.put("CAP_DEVICE_ID", "110203");
            mes.put("CAP_DEVICE_NAME", "井岗路与皖山路交口人像摄像头");
            mes.put("ORDER_INFO", "设备掉线，链接异常");

            for (int i = 0; i < 3; i++) {
                mes.put("WORK_ORDER_ID", new Date().getTime() + "");
                String curDate = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                mes.put("CREATE_TIME", curDate);

                ProducerRecord<Object, Object> record = new ProducerRecord<>(topic, key + "-" + i, mes.toJSONString());
                System.out.println(record);
                producer.send(record);
                Thread.sleep(30000);
            }
        } catch (Exception ignored) {
        }
    }
}
